<?php
namespace Service\Library;

/**
 * Common functions
 */
class LibCommon
{
    /**
     * Get server host
     * @return string
     */
    public static function getHost()
    {
        return self::getServer('HTTP_HOST');
    }

    /**
     * Get referer
     * @return string
     */
    public static function getReferer()
    {
        return self::getServer('HTTP_REFERER');
    }

    /**
     * Get method
     * @return string
     */
    public static function getMethod()
    {
        return self::getServer('REQUEST_METHOD');
    }

    /**
     * Get request_uri
     * @return string
     */
    public static function getRequestUri()
    {
        return self::getServer('REQUEST_URI');
    }

    /**
     * Get server vars
     * @param string $var
     * @param mixed $default
     * @return mixed
     */
    public static function getServer($var, $default = null)
    {
        return isset($_SERVER[$var]) ? $_SERVER[$var] : $default;
    }

    /**
     * Get request vars
     * @param string $var
     * @param mixed $default
     * @return mixed
     */
    public static function getRequest($var, $default = null)
    {
        return self::getVar($_REQUEST, $var, $default);
    }

    /**
     * Get vars
     * @param string $var
     * @param mixed $default
     * @return mixed
     */
    public static function get($var, $default = null)
    {
        return self::getVar($_GET, $var, $default); 
    }

    /**
     * Post vars
     * @param string $var
     * @param mixed $default
     * @return mixed
     */
    public static function post($var, $default = null)
    {
        return self::getVar($_POST, $var, $default);
    }

    /**
     * Get var
     * @param array $array
     * @param string $key
     * @param mixed $default
     * @return mixed
     */
    public static function getVar(& $array, $key, $default = null)
    {
        return isset($array[$key]) ? $array[$key] : $default;
    }
}